package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.text.TextUtils
import android.view.View
import cn.jpush.im.android.api.JMessageClient
import com.buz.hjcdriver.bean.GroupMemberBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.cjt2325.cameralibrary.util.LogUtil
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_groupdetail.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.GroupMemberAdapter
import lium.lbs.lbsim.bean.GroupInfoBean
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.ToastUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.zfw.PotentialFriendActivity
import java.util.ArrayList
import java.util.HashMap

class GroupDetailActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {

    var groupId: String? = null
    var myGroupId: String? = null
    var groupInfoBean: GroupInfoBean? = null
    var url: List<String>? = null
    var gcard: String? = null
    val bfBean = ArrayList<GroupMemberBean>()
    var adapter: GroupMemberAdapter? = null
    var memberRole: String? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_groupdetail
    }


    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("群详情")
        groupId = intent.getStringExtra(GJApp.GROUP_ID)
        myGroupId = intent.getStringExtra(GJApp.MY_GROUP_ID)
        addOnClickListeners(
            R.id.grouo_manager,
            R.id.tv_seemore,
            R.id.ll_toewm,
            R.id.complain,
            R.id.clear_chatremark,
            R.id.ll_group_notice,
            R.id.ll_tochangename,
            R.id.ll_gcard,
            R.id.quit_group,
            R.id.complain
        )
        initRecyclerView()
    }

    private fun initRecyclerView() {
        adapter = GroupMemberAdapter()
        val layoutManager = GridLayoutManager(this, 5)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter!!.setOnItemClickListener(this)
        adapter!!.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能

    }

    override fun getNetWorkData() {
        getDataList()
    }

    fun getDataList() {

        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["gid"] = groupId.toString()
        postData(
            "/group/groupInfo",
            param,
            object : DialogCallback<ResponseBean<GroupInfoBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<GroupInfoBean>>) {
                    setDataList(response.body().data)
                }
            })
    }


    private fun setDataList(groupInfoBean: GroupInfoBean) {
        this.groupInfoBean = groupInfoBean
        tv_groupname.text = groupInfoBean.group.groupName
        group_notice.text = groupInfoBean.group.notice
        tv_groupname.text = groupInfoBean.group.groupName
        memberRole= groupInfoBean.group.memberRole
        if (!groupInfoBean.group.ownerId.equals(SpUtils.getInstance().get(Constants.memberId, "") as String)) {
            grouo_manager.isEnabled = false
            ll_group_notice.isEnabled = false
            ll_tochangename.isEnabled = false
            gourp_name_to.visibility = View.GONE
            notion_to.visibility = View.GONE
            group_m_to.visibility = View.GONE
        }
        if (groupInfoBean.group.memberRole.equals("1")){
            quit_group.visibility = View.GONE
        } else {
            quit_group.visibility = View.VISIBLE
        }
        gcard = groupInfoBean.group.gcard
        my_card.text = gcard
        url = groupInfoBean.group.headImage
        LogUtils.e("参数--" + groupInfoBean.group.memberList.size)
        if (groupInfoBean.group.memberList != null) {
            var list = arrayListOf<GroupMemberBean>()
            if (groupInfoBean.group.memberList.size >= 10) {
                for (index in 0..8) {
                    list.add(
                        GroupMemberBean(
                            if (TextUtils.isEmpty(groupInfoBean.group.memberList.get(index).gcard)) {
                                groupInfoBean.group.memberList.get(index).nickName
                            } else {
                                groupInfoBean.group.memberList.get(index).gcard
                            },
                            groupInfoBean.group.memberList.get(index).headImage,
                            groupInfoBean.group.memberList.get(index).memberId
                        )
                    )
                }
            } else {
                for (index in 0..groupInfoBean.group.memberList.size - 1) {
                    list.add(
                        GroupMemberBean(
                            if (TextUtils.isEmpty(groupInfoBean?.group.memberList?.get(index)?.gcard)) {
                                groupInfoBean.group?.memberList?.get(index).nickName
                            } else {
                                groupInfoBean.group?.memberList?.get(index)?.gcard
                            }
                            ,
                            if (!TextUtils.isEmpty(groupInfoBean.group?.memberList?.get(index)?.headImage)) {
                                groupInfoBean.group?.memberList?.get(index)?.headImage
                            } else {
                                ""
                            },
                            groupInfoBean.group?.memberList?.get(index)?.memberId
                        )
                    )
                }
            }
            bfBean.clear()
            if (groupInfoBean.group.memberRole.equals("1") ||groupInfoBean.group.memberRole.equals("2")){
                list.add(GroupMemberBean("11", "11", "11"))
            }
            bfBean.addAll(list)
            LogUtils.e("body-bfBean.size--" + bfBean.size)
            adapter!!.data.clear()
            adapter!!.data.addAll(list)
            adapter!!.notifyDataSetChanged()
        }

    }

    override fun onClick(v: View?) {
        val intent = Intent()
        super.onClick(v)
        when (v!!.id) {
            R.id.ll_gcard -> {
                intent.setClass(this, GroupCardActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
                intent.putExtra("gcard", gcard)
                startActivityForResult(intent, 100)
            }
            R.id.grouo_manager -> {
                intent.setClass(this, SetGroupActivity::class.java)
                intent.putExtra(GJApp.GROUP_ID, groupId)
                intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
                intent.putExtra("memberRole", memberRole)
                startActivity(intent)
            }
            R.id.tv_seemore -> {
                intent.setClass(this, GroupMemberListActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
                startActivity(intent)
            }
            R.id.ll_toewm -> {
                intent.setClass(this, GroupRQCodeActivity::class.java)
                intent.putExtra("QRcode", groupInfoBean)
                intent.putStringArrayListExtra("urls", url as ArrayList<String>?)
                startActivity(intent)
            }

            R.id.complain -> {
                intent.setClass(this, CompliantActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
                startActivityForResult(intent, 100)
            }
            R.id.ll_group_notice -> {
                intent.setClass(this, GroupNoticeActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
                startActivityForResult(intent, 100)
            }
            R.id.clear_chatremark -> {
                JMessageClient.deleteGroupConversation(groupId!!.toLong());
                ToastUtils.showToast("已经清空群信息~")
                setResult(203)
            }
            R.id.quit_group -> {
                getDataDelete()
            }
            R.id.ll_tochangename -> {
                intent.setClass(this, GroupNameActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
                startActivityForResult(intent, 100)
            }

        }


    }


    fun getDataDelete() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["groupId"] = myGroupId.toString()
        postData(
            "/groupmember/memberOutGroup",
            param,
            object : DialogCallback<ResponseBean<GroupInfoBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<GroupInfoBean>>) {
                    ToastUtils.showToast(response.body().msg)
                    setResult(203)
                    finish()
                }
            })
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtil.e("onActivityResult--resultCode" + data + "requestCode" + requestCode + "-body")
        if (requestCode == 100 && resultCode == 200) {
            LogUtils.e("myGroupId.toString()--body" + groupId.toString())
            getDataList()
        }
        if (requestCode == 100 && resultCode == 201) {
            LogUtils.e("myGroupId.toString()--" + groupId.toString())
            getDataList()
            setResult(201)
        }
        if (requestCode == 100 && resultCode == 202) {//刷新聊天页面
            LogUtils.e("myGroupId.toString()--" + groupId.toString())
            getDataList()
            setResult(202)
        }
        if (requestCode == 100 && resultCode == 203) {//刷新聊天类表
            LogUtils.e("myGroupId.toString()--" + groupId.toString())
            getDataList()
            setResult(203)
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent()
        if (!bfBean[position].id.equals("11")) {
            intent.setClass(this, PotentialFriendActivity::class.java)
            intent.putExtra("fid", bfBean[position].id)
            startActivity(intent)
        } else {
            groupId = intent.getStringExtra(GJApp.MY_GROUP_ID)
            intent.setClass(this, ChooseMemberActivity::class.java)
            intent.putExtra(GJApp.MY_GROUP_ID, myGroupId)
            startActivity(intent)
        }

    }


}